package com.example.chat.common.utils;

import com.alibaba.fastjson.JSON;
import com.example.chat.common.base.PageData;

import java.util.List;
import java.util.Objects;

/**
 * @Description: 自定义数据转换工具
 * @Author: mayanhui
 * @Date: 2024-07-16  17:08
 */
public class ConvertUtils {
    public static <T> PageData<T> toList(PageData source, Class<T> target) {
        if (source == null) {
            return null;
        } else {
            List<T> targetList = JSON.parseArray(JSON.toJSONString(source.getList()), target);
            Long total = source.getTotalCount();
            return new PageData(total, targetList);
        }
    }

    public static <T> List<T> toList(List source, Class<T> target) {
        if (source == null) {
            return null;
        } else {
            return JSON.parseArray(JSON.toJSONString(source), target);
        }
    }

    public static <T, Q> T of(Q q, Class<T> targetClass) {
        if (Objects.isNull(q)) {
            return null;
        } else {
            return JSON.parseObject(JSON.toJSONString(q), targetClass);
        }
    }
}
